package itextpdf;

import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.extra.template.TemplateException;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.layout.font.FontProvider;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

import java.io.*;
import java.util.Map;

public class PDFUtil {
    private volatile static Configuration configuration;

    private static final String DEST = "target/HelloWorld_CN_HTML.pdf";
    private static final String HTML = "template.html";
    private static final String FONT = "SimHei.ttf";

    static {
        if (configuration == null) {
            synchronized (PDFUtil.class) {
                if (configuration == null) {
                    configuration = new Configuration(Configuration.VERSION_2_3_32);
                }
            }
        }
    }

    /**
     * 使用 iText 生成 PDF 文档
     *
     * @param htmlTmpStr   html 模板文件字符串
     * @param fontFilePath 所需字体文件(相对路径+文件名)
     * @param waterImgPath 水印图片路径
     * @param waterContent 水印文字内容
     */
    public static byte[] createPDF(String htmlTmpStr, String fontFilePath, String waterImgPath, String waterContent) throws FileNotFoundException {
        String path = JavaToPdf.class.getClass().getResource("/itextpdf").getPath();
        File pdfDest = new File(DEST);
        FontProvider fontProvider = new DefaultFontProvider();
        fontProvider.addFont(path+File.separatorChar+FONT);
        ConverterProperties props = new ConverterProperties().setFontProvider(fontProvider);
        HtmlConverter.convertToPdf(htmlTmpStr, new FileOutputStream(pdfDest),props);
        return null;

    }


    public static String freemarkerRender(Map<String, Object> dataMap, String ftlFilePath) {
        configuration.setDefaultEncoding("UTF-8");
        configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        configuration.setLogTemplateExceptions(false);
        configuration.setWrapUncheckedExceptions(true);
        Writer out = new StringWriter();
        try {
            Template template = new Template("", PDFUtil.returnReaderStream(ftlFilePath), configuration);
            template.process(dataMap, out);
            out.flush();
            return out.toString();
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        } catch (freemarker.template.TemplateException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;

    }

    /**
     * 根据文件相对路径返回一个 BufferedReader 流
     *
     * @param filePath
     * @return
     * @throws IOException
     */
    public static BufferedReader returnReaderStream(String filePath) throws IOException {
        return new BufferedReader(new InputStreamReader(new ClassPathResource(filePath).getStream()));
    }

    /**
     * 根据文件相对路径返回一个 BufferedReader 流
     *
     * @param filePath
     * @return
     * @throws IOException
     */
    public static InputStream returnInputStream(String filePath) throws IOException {
        return new ClassPathResource(filePath).getStream();
    }


}
